package com.hotel.service.Impl;

import com.hotel.mapper.BillMapper;
import com.hotel.pojo.Bill;
import com.hotel.pojo.User;
import com.hotel.service.BillService;
import com.hotel.utils.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BillServiceImpl implements BillService {
    @Autowired
    private BillMapper billMapper;

    //查询所有记录
    @Override
    public List<Bill> selectAllBill() {
        return billMapper.selectBill();
    }

    //添加入住收益记录
    @Override
    public ResultInfo checkIn(Bill bill) {
        try {
            billMapper.checkIn(bill);
            return new ResultInfo(200, "成功入住");
        } catch (Exception e) {
            System.out.println("入住失败");
        }
        return new ResultInfo(500, "入住失败", null);
    }

    //获取房间单价
    @Override
    public Integer getRoomPrice(Integer roomId) {
        return billMapper.roomPrice(roomId);
    }


    //收支情况
    @Override
    public ResultInfo billVisual() {
        long expenditure = 0;    //支出
        long income = 0;     //收入

        // 定义空list存储玫瑰图所需数据
        List<Map<String, Object>> billList = new ArrayList<>();
        // 查询到商品名称及对应的价格
        List<Bill> bills = billMapper.selectBill();
        List<String> types = billMapper.selectBillType();

        // foreach遍历godsVisual进List，  南丁格尔图所需的参数类型：List<Map<String,Object>>
        for (Bill bill : bills) {
            if (bill.getType().equals("支出")) {
                expenditure += bill.getMoney();
            } else {
                income += bill.getMoney();
            }
        }
        for (String type : types) {
            // 定义空map存储玫瑰图所需数据
            Map<String, Object> billsMap = new HashMap<>();
            if (type.equals("支出")) {
                billsMap.put("name", type);
                billsMap.put("value", expenditure);
            } else {
                billsMap.put("name", type);
                billsMap.put("value", income);
            }
            billList.add(billsMap);
        }
        System.out.println(billList);
        return new ResultInfo(200, "可视化分析！", billList);

    }

    //财务收支详情
    @Override
    public ResultInfo billInfoVisual() {
        // 定义空list存储图所需数据,嵌套列表
        List<List<Map<String, Object>>> lists = new ArrayList<>();  //外层
        List<Map<String, Object>> billList = new ArrayList<>(); //内层
//        List<Map<String, Object>> billList01 = new ArrayList<>(); //内层01存资金来源
        //查询数据
        List<String> billType = billMapper.selectBillType();
        List<String> billNote = billMapper.selectBillNote();
        Map<String, Object> billNoteMap = new HashMap<>();

        billNoteMap.put("type", billNote);
        billList.add(billNoteMap);

        for (String type : billType) {
            int values = 0; //总金额
            Map<String, Object> billsMap = new HashMap<>();
            List<Integer> noteMoney = new ArrayList<>();
            for (String note : billNote) {

                try {
                    values = billMapper.selectBillByTypeNote(type, note);
                } catch (NullPointerException e) {
                    values = 0;
                } catch (Exception e) {
                    System.out.println("财务其他错误");
                }
                noteMoney.add(values);
            }
            billsMap.put("name", type);
            billsMap.put("type", "bar");
            billsMap.put("data", noteMoney);
            billList.add(billsMap);
        }
//        lists.add(billList);
//        System.out.println(billList);
        return new ResultInfo(200, "可视化分析！", billList);
    }

}
